<?php

//注册两个新的Sidebar到主题
register_sidebar( array( 'name' => __('Archive', 'googlecode'), 'id' => 'archive-aside', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Page', 'googlecode'), 'id' => 'page-aside', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );

add_action( 'hybrid_after_container', 'googlecode_get_archive_aside');
function googlecode_get_archive_aside() {

	$templates = array( 'aside-archive.php' );

	locate_template( $templates, true );
}

add_action( 'hybrid_after_container', 'googlecode_get_page_aside');
function googlecode_get_page_aside() {

	$templates = array( 'aside-page.php' );

	locate_template( $templates, true );
}

//添加搜索框
add_filter('hybrid_site_description','hc_print_search_box');
function hc_print_search_box($desc){
    ob_start();
    get_search_form();
    $searchbox = ob_get_contents();
    ob_end_clean();
    return $desc . $searchbox;
}

//Menu Tab自定义结构，下面的函数可以自定义wp_list_pages输出的html结构，没什么神秘的，用正则进行了替换而已。
add_filter('wp_page_menu','hc_add_span_wrap_to_menu_link');
function hc_add_span_wrap_to_menu_link($menu){
    $menu = preg_replace('#(<li[^>]+><a[^>]+>)([^<]+)(</a></li>)#','\\1<span>\\2</span>\\3',$menu);
    $menu = preg_replace('#</ul></div>#','',$menu);
    $menu .= '<li class="page_item"><a title="Forum" href="' . get_option('home') . '/forum"><span>Forums</span></a></li></ul></div>';
    return $menu;
}

//Menu Sub Panel注入
add_filter('hybrid_after_page_nav','hc_add_menu_subpanel');
function hc_add_menu_subpanel(){
    echo '</div>';
    echo '<div id="menu-sub-panel">';
}

//在页面footer注入js文件
add_action('hybrid_after_html','hc_print_js');
function hc_print_js(){
    $title_tab = WP_CONTENT_URL . '/themes/googlecode/js/title-tab.js';
    $toggle_widgets =  WP_CONTENT_URL . '/themes/googlecode/js/toggle-widgets.js';
    $rss_tab = WP_CONTENT_URL . '/themes/googlecode/js/rss-tab.js';
    $hide_sidebar = WP_CONTENT_URL . '/themes/googlecode/js/hide-sidebar.js';
    echo '<script type="text/javascript" src="',$title_tab,'"></script>';
    echo '<script type="text/javascript" src="',$toggle_widgets,'"></script>';
    echo '<script type="text/javascript" src="',$rss_tab,'"></script>';
    echo '<script type="text/javascript" src="',$hide_sidebar,'"></script>';
}

//彻底替换原有的byline函数
function googlecode_byline() {
	global $post;

	if ( !is_page() && !is_attachment() ) :
		$byline = '<p class="byline">';
		$byline .= sprintf('<span class="text">By</span> %1$s <span class="text">on</span> %2$s', '<span class="author vcard">' . get_the_author() . '</span>', '<abbr class="published" title="' . sprintf( get_the_time('l, F jS, Y, g:i a')) . '">' . sprintf(get_the_time( 'M jS, Y')) . '</abbr>' );
		if ( current_user_can( 'edit_posts' ) )
			$byline .= ' <span class="separator">|</span> <span class="edit"><a href="' . get_edit_post_link( $post->ID ) . '" title="' . __('Edit post', 'hybrid') . '">' . __('Edit', 'hybrid') . '</a></span>';
		if (function_exists('the_user_views')){
            $views = the_user_views('', false);
            $byline .= ' <span class="separator">|</span> <span class="postviews">'
                . $views . _n(' View', ' Views', $views) . '</span>';
        }else if(function_exists('the_views')){
            $views = the_views('', false);
            $byline .= ' <span class="separator">|</span> <span class="postviews">'
                    . $views . _n(' View', ' Views', $views) . '</span>';
        }
        $byline .= '</p>';
	endif;
	echo apply_filters( 'googlecode_byline', $byline );
}

//摘除原有的hook，挂载新hook的方法
add_action('hybrid_head','change_hook_functions');
function change_hook_functions(){
    remove_action('hybrid_before_entry','hybrid_byline');
    add_action('hybrid_before_entry','googlecode_byline');
}

//首页的post meta里不显示tag list
add_action('wp_head','hide_tags_in_postmeta');
function hide_tags_in_postmeta(){
    if(is_home()):
    ?>
<style type="text/css">.entry-meta .tags{display:none;}</style>
    <?php
    endif;
}

wp_enqueue_script('jquery');
?>